package service

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"tFilm4/internal/model"
	"tFilm4/internal/model/entity"
	"tFilm4/internal/service/internal/dao"
	"tFilm4/internal/service/internal/do"
)

type sMovieHall struct{}

var insMovieHall = sMovieHall{}

func MovieHall() *sMovieHall {
	return &insMovieHall
}

func (s *sMovieHall) CreateMovieHall(ctx context.Context, in model.CreateMovieHallInput) (err error) {
	return dao.MovieHall.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		insert, err := dao.SeatTable.Ctx(ctx).Data(do.SeatTable{
			Rows:       in.Rows,
			Cols:       in.Cols,
			Table:      in.Table,
			TotalCount: in.TotalCount,
			EmptyCount: in.EmptyCount,
		}).Insert()
		if err != nil {
			return err
		}
		seatId, err := insert.LastInsertId()
		if err != nil {
			return err
		}
		_, err = dao.MovieHall.Ctx(ctx).Data(do.MovieHall{
			HallName:        in.HallName,
			HallSeatId:      seatId,
			HallDescription: in.HallDescription,
		}).Insert()
		return err
	})
}

func (s *sMovieHall) GetHallList(ctx context.Context, in model.GetHallListInput) (count int, MovieHallInfos []model.MovieHallInfo, err error) {
	err = dao.MovieHall.Ctx(ctx).WhereLike("hall_id", "%"+in.SearchKey+"%").WhereOrLike("hall_name", "%"+in.SearchKey+"%").Page(in.PageNum, in.PageSize).ScanList(&MovieHallInfos, "MovieHall")
	if err != nil {
		return
	}
	err = dao.SeatTable.Ctx(ctx).Where("table_id", gdb.ListItemValuesUnique(&MovieHallInfos, "MovieHall", "HallSeatId")).ScanList(&MovieHallInfos, "SeatTable", "MovieHall", "table_id:HallSeatId")
	if err != nil {
		return
	}
	count, err = dao.MovieHall.Ctx(ctx).Count()
	return
}

func (s *sMovieHall) DelMovieHall(ctx context.Context, id int64) (err error) {
	return dao.MovieHall.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		m := &entity.MovieHall{}
		err := dao.MovieHall.Ctx(ctx).WherePri("hall_id", id).Scan(&m)
		if err != nil {
			return gerror.Newf(`要删除的影厅编号%d不存在`, id)
		}
		_, err = dao.SeatTable.Ctx(ctx).Delete("table_id", m.HallSeatId)
		if err != nil {
			return gerror.Newf(`要删除的座位表编号%d不存在`, m.HallSeatId)
		}
		_, err = dao.MovieHall.Ctx(ctx).Delete("hall_id", id)
		if err != nil {
			return gerror.Newf(`要删除的影厅编号%d不存在`, id)
		}
		return err
	})
}

func (s *sMovieHall) UpdateMovieHall(ctx context.Context, hall *entity.MovieHall, table *entity.SeatTable) (err error) {
	return dao.MovieHall.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := dao.MovieHall.Ctx(ctx).Data(g.Map{
			"hall_name":        hall.HallName,
			"hall_description": hall.HallDescription,
		}).Where("hall_id", hall.HallId).Update()
		if err != nil {
			return gerror.New(`影厅修改失败`)
		}
		_, err = dao.SeatTable.Ctx(ctx).Data(g.Map{
			"rows":        table.Rows,
			"cols":        table.Cols,
			"table":       table.Table,
			"total_count": table.TotalCount,
			"empty_count": table.EmptyCount,
		}).Where("table_id", table.TableId).Update()
		if err != nil {
			return gerror.New(`影厅座位修改失败`)
		}
		return err
	})
}
